import axios from 'axios'; // 引入axios
import store from '@/store'

let baseUrl = "http://localhost:8082/";
// 创建axios实例
const httpService = axios.create({
    // url前缀-'http:xxx.xxx'
    // baseURL: process.env.BASE_API, // 需自定义
    baseURL: baseUrl,
    // 请求超时时间
    timeout: 5000 // 需自定义
});

// 添加请求和响应拦截器
// 添加请求拦截器
httpService.interceptors.request.use(function (config) {
    config.headers['request-startTime'] = new Date().getTime()
    config.headers.token = store.getters.GET_TOKEN
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// const CancelToken = axios.CancelToken;
// const pending = [];
// // axios 相关配置修改
// const defaultConfig = {}
// defaultConfig.cancelToken = new CancelToken(function executor(c) {
//     pending.push(c);
// });
// axios(defaultConfig);

let requestDuration = ''
// 添加响应拦截器
httpService.interceptors.response.use(function (response) {
    // 对相应结果做些什么
    const start = response.config.headers['request-startTime']
    const currentTime = new Date().getTime()
    requestDuration = ((currentTime - start));
    return response;
}, function (error) {
    // console.log(error)
    // let errCode = error.response.data.code;
    // let errMsg = error.response.data.msg;
    // if (errCode === 401 && errMsg === '认证失败，请登录！') {
    //     // 登录超时，接口请求多次，导致弹框会多次出现，方案：只弹一次
    //     while (pending.length > 0) {
    //         pending.pop()('请求中断');
    //         store.dispatch('logout')
    //         ElMessage.error("请求超时，请重新登录")
    //     }
    // }
    return Promise.reject(error);
});

/*网络请求部分************************************************/

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        // setTimeout(()=>{
        httpService({
            url: url,
            method: 'get',
            data: params
        }).then(response => {
            // console.log(response)
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
        // },5000)
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        // setTimeout(()=>{
        httpService({
            url: url,
            method: 'post',
            data: params
        }).then(response => {
            // console.log(response)
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
        // },5000)
    });
}

/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

// 下载excel文件
export function exportExcel(url) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            responseType: 'blob', // 使用blob下载
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    })
}


export function getServerUrl() {
    return baseUrl;
}

export default {
    get,
    post,
    exportExcel,
    fileUpload,
    getServerUrl
}
